package utils;

import controller.Controller;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lincl on 2018/12/6.
 */
public class Global {
    public static final String Exe_Name = "svnPacking";
    public static final String DOWN_LOAD_PATH = "https://gitee.com/lincl/svnPacking/raw/master/resource/update/";
    public static final String CUR_VERSION = "v1.0.1";


    public static int number = 0;
    public static final String CONFIG_DIR = "\\.svnpacking";
    public static final String FILE_TYPE = ".dat";
    public static String userHome = System.getProperty("user.home");

    public static Map<String, ConfigBean> configBeanMap = new HashMap<>();

    public static ConfigBean curConfigBean;



    public static ConfigBean getConfigBean(String name){
        curConfigBean = configBeanMap.get(name);
        return curConfigBean;
    }

    public static int getNumber(){
        number += 1;
        return number;
    }


    public static void init(){
        File file = new File(userHome + CONFIG_DIR);
        if(!file.exists()){
            file.mkdir();
        } else {
            ConfigBean bean;
            FileInputStream in = null;
            for(File cfgFile: file.listFiles()){
                try {
                    if(!cfgFile.getName().endsWith(FILE_TYPE)){
                        continue;
                    }
                    in = new FileInputStream(cfgFile);
                    ObjectInputStream objIn = new ObjectInputStream(in);
                    bean = (ConfigBean) objIn.readObject();
                    configBeanMap.put(
                            cfgFile.getName().substring(0, cfgFile.getName().lastIndexOf(".")),
                            bean);
                    in.close();
//                    BufferedReader bufferedReader = new BufferedReader(new FileReader(cfgFile));
//                    String s = bufferedReader.readLine();
//                    bean = new ConfigBean();
//                    while (s!=null && !"".equals(s)){
//                        bean.setProperty(s);
//                        s = bufferedReader.readLine();
//                    }
//                    configBeanMap.put(
//                            cfgFile.getName().substring(0, cfgFile.getName().lastIndexOf("\\.")),
//                            bean);
                } catch (Exception e) {
                    if(in!=null){
                        try {
                            in.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                    e.printStackTrace();
                }
            }
        }


    }

    public static void saveCfg(ConfigBean bean){
        String oldName = Global.curConfigBean.getName();
        Controller.controller.reName(oldName, bean.getName());
        Global.setCurConfigBean(bean);
        Global.configBeanMap.put(bean.getName(), bean);

        if(!oldName.equals(bean.getName())){
            File oldFile = new File(userHome + CONFIG_DIR + "\\" + oldName + FILE_TYPE);
            if(oldFile.exists()){
                oldFile.delete();
            }
        }

        File file = new File(userHome + CONFIG_DIR + "\\" +  bean.getName() + FILE_TYPE);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            ObjectOutputStream objOut=new ObjectOutputStream(out);
            objOut.writeObject(bean);
            objOut.flush();
            objOut.close();
            MonologUtils.alert("文件保存成功!");
        } catch (IOException e) {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            MonologUtils.alert("文件保存失败!");
            e.printStackTrace();
        }
    }

    public static ConfigBean getCurConfigBean() {
        return curConfigBean;
    }

    public static void setCurConfigBean(ConfigBean curConfigBean) {
        Global.curConfigBean = curConfigBean;
    }
}
